﻿using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Padua
{
    public partial class PaduaPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        string Id;
        #endregion

        #region 构造函数
        public PaduaPrint(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = id;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\栓塞症风险评估表.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 获取链接报表事件
        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.PaduaScore);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id == Convert.ToInt32(this.Id)).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    PaduaModel model = XmlUtil.XMLToObject<PaduaModel>(record1.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,   //患者姓名
                        PatientInfo.SEX,
                        PatientInfo.AGE,   //年龄
                        PatientInfo.NURSE_CELL_NAME,
                        PatientInfo.DEPARTMENT_NAME,
                        PatientInfo.BED_NO,
                        PatientInfo.PATIENT_ID,   //住院号

                        model.PaduaSumScore,

                        model.PaduaS1.Split('#')[0].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[1].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[2].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[3].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[4].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[5].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[6].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[7].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[8].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[9].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[10].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[11].Equals("1") ? "1" : string.Empty,
                        model.PaduaS1.Split('#')[12].Equals("1") ? "1" : string.Empty,

                        model.PaduaS2.Equals("1") ? "2" : string.Empty,

                        model.PaduaS3.Split('#')[0].Equals("1") ? "3" : string.Empty,
                        model.PaduaS3.Split('#')[1].Equals("1") ? "3" : string.Empty,
                        model.PaduaS3.Split('#')[2].Equals("1") ? "3" : string.Empty,
                        model.PaduaS3.Split('#')[3].Equals("1") ? "3" : string.Empty

                        );
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("XingBie", typeof(string));
            dt.Columns.Add("Age", typeof(string));
            dt.Columns.Add("CellName", typeof(string));
            dt.Columns.Add("DepName", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));

            dt.Columns.Add("PaduaSumScore", typeof(string));

            dt.Columns.Add("PA1", typeof(string));
            dt.Columns.Add("PA2", typeof(string));
            dt.Columns.Add("PA3", typeof(string));

            dt.Columns.Add("PA4", typeof(string));
            dt.Columns.Add("PA5", typeof(string));
            dt.Columns.Add("PA6", typeof(string));
            dt.Columns.Add("PA7", typeof(string));
            dt.Columns.Add("PA8", typeof(string));
            dt.Columns.Add("PA9", typeof(string));
            dt.Columns.Add("PA10", typeof(string));
            dt.Columns.Add("PA11", typeof(string));
            dt.Columns.Add("PA12", typeof(string));
            dt.Columns.Add("PA13", typeof(string));

            dt.Columns.Add("PB1", typeof(string));

            dt.Columns.Add("PC1", typeof(string));
            dt.Columns.Add("PC2", typeof(string));
            dt.Columns.Add("PC3", typeof(string));
            dt.Columns.Add("PC4", typeof(string));
        }

        #endregion

        #region 窗体加载启动锐浪报表
        private void PaduaPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}
